/*************************************************
Author: will
Date: 10/24/2012
Description:
Notes:
*************************************************/

#ifndef __CCrystalConfig_h__
#define __CCrystalConfig_h__

#include "../equip/CEquipConfig.h"
#include "CDropItemSubType.h"

class CCrystalConfigData
{
public:
    enum CRYSTAL_POS_LIMIT_TYPE
    {

    };

	CCrystalConfigData()
        :_bHasConfig(false)
    {
        _usDecompFragMin = 0;
        _usDecompFragMax = 0;
        _uiWareLimit = 0;
        _ucPotenAttr = 0;
        _iPotenValue = 0;
        _ucAttrType = 0;
        _uiAttrValue = 0;

	}

	virtual ~CCrystalConfigData()
    {
	}

    int SetCanWarePos(unsigned char ucWarePos)
    {
        if (ucWarePos > EQUIP_POS_MAX)
        {
            CT_ERROR(("set can ware pos error, pos %d", ucWarePos));
            return RET_SYSTEM_FAIL;
        }

        _uiWareLimit |= (1 << ucWarePos);

        return 0;
    }

    bool IsCanWare(unsigned char ucWarePos)
    {
        if (ucWarePos > EQUIP_POS_MAX)
        {
            return false;
        }

        return (_uiWareLimit & (1 << ucWarePos));
    }

public:
    bool        _bHasConfig;

	unsigned short _usCrystalConfigID;
    unsigned short _usLevel;  //魔晶自身的等级. 取值范围: [1,10]
    CTSizeString<DROP_ITEM_CFG_SUBTYPE_STR_LEN> _szDropItemSubType;
    CTSizeString<MAX_ICON_NAME_LEN> _szIconName;

    unsigned char  _ucAttrType;
    unsigned int   _uiAttrValue;
    unsigned int   _uiWareLimit;

    unsigned char  _ucPotenAttr;
    int            _iPotenValue;

    unsigned short _usDecompFragMin;  //分解后魔晶的最小个数
    unsigned short _usDecompFragMax;  //分解后魔晶的最大个数cc
    int   _iDecomPrice;     //分解的价格

    unsigned short _usConstructCrystalID;
    unsigned short _usRequiredLevel; //合成需要主角的等级
    unsigned int   _uiSilverBuyPrice;
    unsigned int   _uiSilverSellPrice;
    unsigned int   _uiGoldBuyPrice;
    unsigned int   _uiContriPrice;   //贡献度价格(家族商店)
    unsigned int   _uiInstanceType;  //缺省的掉落关卡类型
    unsigned int   _uiInstanceId;    //缺省的掉落关卡ID
};

class CCrystalFragConfig
{
public:
    CCrystalFragConfig () : _bHasConfig(false)
    {

    }

    bool _bHasConfig;
    unsigned short _usCrystaFraglId;    // 魔晶碎片ID
    unsigned int   _uiSilverBuyPrice;   // 银币买价
    unsigned int   _uiSilverSellPrice;  // 银币卖价
    int   _iBuyContPrice;  // 贡献度买家
};

class CCrystalConfig
{
public:
	CCrystalConfig()
    {
	}

	virtual ~CCrystalConfig()
    {
	}

    static size_t CountSize()
    {
        return CDropItemSubTypeConfig::CountSize(MAX_CRYSTAL_CONFIG_COUNT);
    }

    int Init(const char* sBuff)
    {
        return _stDropItemSubTypeConfig.Init(sBuff, MAX_CRYSTAL_CONFIG_COUNT);
    }

    int AddConfig(CCrystalConfigData& stConfigData)
    {
        int iRet = 0;

        if (stConfigData._usCrystalConfigID > MAX_CRYSTAL_CONFIG_COUNT)
        {
            CT_ERROR(("add crystal config error, id %d", stConfigData._usCrystalConfigID));
            return RET_SYSTEM_FAIL;
        }

        if (_astCrystalConfigDatas[stConfigData._usCrystalConfigID]._bHasConfig)
        {
            CT_ERROR(("add duplicated crystal config error, id %d", stConfigData._usCrystalConfigID));
            return RET_SYSTEM_FAIL;
        }

        _astCrystalConfigDatas[stConfigData._usCrystalConfigID] = stConfigData;
        _astCrystalConfigDatas[stConfigData._usCrystalConfigID]._bHasConfig = true;

        //添加掉落类型配置
        if (stConfigData._szDropItemSubType.GetDataLen() > 0)
        {
            CDropItemSubTypeConfigData stItemDropSubTypeData;
            stItemDropSubTypeData._iItemID = stConfigData._usCrystalConfigID;
            iRet = _stDropItemSubTypeConfig.AddSubType(stConfigData._szDropItemSubType, stItemDropSubTypeData);
            CHECK_RETURN(iRet);
        }

        return 0;
    }

    //魔晶属性加成
    int GetCrystalAttrAdd (unsigned short usCrystalConfigID, CAttrPair & stAttr)
    {
        CCrystalConfigData stConfigData;

        int iRet = GetConfig(usCrystalConfigID, stConfigData);
        CHECK_RETURN(iRet);

        if (stConfigData._usLevel > MAX_CRYSTAL_LEVLE || stConfigData._usLevel < 1)
        {
            return ERROR_CRYSTAL_CONFIG;
        }
#if 0
        unsigned short ausAttrAdd[] = {30, 45, 60, 80, 100, 125, 150, 180, 210, 250}; //千分位

        stAttr._ucType = stConfigData._ucAttrType;
        stAttr._iValue = ausAttrAdd[stConfigData._usLevel - 1];
#endif

        stAttr._ucType = stConfigData._ucAttrType;
        stAttr._iValue = (int)stConfigData._uiAttrValue;
        return 0;
    }

    int GetConfig(unsigned short usCrystalConfigID, CCrystalConfigData& stConfigData)
    {
        if (usCrystalConfigID > MAX_CRYSTAL_CONFIG_COUNT)
        {
            return ERROR_ITEM_NO_CRYSTAL_CONFIG;
        }

        if (!_astCrystalConfigDatas[usCrystalConfigID]._bHasConfig)
        {
            return ERROR_ITEM_NO_CRYSTAL_CONFIG;
        }

        stConfigData = _astCrystalConfigDatas[usCrystalConfigID];
        return 0;
    }

    int GetConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
                        CCrystalConfigData& stConfigData)
    {
        int iRet = 0;

        CDropItemSubTypeConfigData stItemDropSubTypeData;
        iRet = _stDropItemSubTypeConfig.GetRandomData(szKey, stItemDropSubTypeData);
        CHECK_RETURN(iRet);

        unsigned short usCrystalConfigID = (unsigned short)stItemDropSubTypeData._iItemID;
        return GetConfig(usCrystalConfigID, stConfigData);
    }

    int GetLevelOneCrystalSubType (CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey)
    {
        for (int i = 0; i < MAX_CRYSTAL_CONFIG_COUNT; i++)
        {
            if (_astCrystalConfigDatas[i]._usLevel == 1)
            {
                szKey = _astCrystalConfigDatas[i]._szDropItemSubType;
                return 0;
            }
        }

        return ERROR_ITEM_NO_CRYSTAL_CONFIG;
    }

public:
    CTLib::CTArray<CCrystalConfigData, MAX_CRYSTAL_CONFIG_COUNT + 1> _astCrystalConfigDatas;
    CDropItemSubTypeConfig _stDropItemSubTypeConfig;
    CCrystalFragConfig _stCrystalFragCfg;
};


#endif // __CCrystalConfig_h__

